/**
 * @file  备注
 * @author hj
 */
import Vue from 'vue';
import resource from 'vue-resource';
var nativeApi = require('nativeApi');
import {APIS} from '../../js/config';

Vue.use(resource);

export default {
  // 每个store第一个属性必须为state，state可以保存多个数据状态，state可以被多个组件绑定来同步状态
  state: {
    teamList: [],
    mainOwner: {},
    teamAddList: [],
    formJson: {},
    editJson: {
      showOperation: false,
      operaCss: {
        bottom: '-196px',
        opacity: 1,
        display: 'none'
      },
      backCss: {
        '-webkit-transition': 'all .4s ease',
        'transition': 'all .4s ease',
        'opacity': 0
      },
      slidEle: '',
      index: '',
      hideDe: false
    },
    isEdit: true,
    entityType: '', //  'company':公司团队;其他的自己定义……
    entityName: '', //  对应字段名称'companyId'……
    entityId: '', //  对应id
    pageTitle: '', //  '公司团队'、'项目团队'、'基金管理团队'、''
    requestData: {
      'url': '',
      'deleteOrAddUrl': '',
      'param': {}
    }  //  请求数据
  },
  scroll: {
    person: {
      dataList: [],
      hasMore: true,
      scroll: {}
    }
  },
  param: {
    person: {
      'q': '',
      // 'projectsId': '',
      'pageNo': 1,
      'pageSize': 10
    }
  },
  /* 翻页相关 */
  setScroll: function (obj, scroll) {
    this.scroll[obj].scroll = scroll;
  },
  listRefresh: function (obj) {
    /* 刷新 */
    this.param[obj].pageNo = 1;
    this.person('refresh');
  },
  listLoadMore: function (obj) {
    /* 加载更多 */
    this.param[obj].pageNo++;
    this.person('loadMore');
  },
  setEdit: function (flag) {
    this.state.isEdit = flag;
  },
  team: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: self.state.requestData.url + self.state.entityId + '/teams',
      param: '',
      callback: function (result) {
        if (result && result.code === 200) {
          var lists = result.data;
          self.state.teamList = lists;
          for (var i = 0; i < lists.length; i++) {
            if (lists[i].isOwner === 1) {
              self.state.mainOwner = lists[i];
            }
          }
          setTimeout(function () {
            mui('#OA_task_2').on('slideleft', '.mui-table-view-cell', function (event) {
              self.state.editJson.slidEle = this;
              self.state.editJson.index = self.state.editJson.slidEle.getAttribute('index');
              setTimeout(function () {
                self.state.editJson.showOperation = true;
                self.state.editJson.operaCss = {
                  bottom: '0',
                  opacity: 1,
                  display: 'block'
                };
                self.state.editJson.backCss.opacity = 1;
                setTimeout(function () {
                  mui.swipeoutClose(self.state.editJson.slidEle);
                }, 300)
              }, 10);
            });
          }, 100);
        } else {
          mui.alert('数据获取失败！', '提示', function () {
          });
        }
      }
    });
  },
  /* 获取请求地址 */
  getUrl: function (cb) {
    var self = this;
    var url = '';
    var entityName = '';
    var pageTitle = '';
    var deleteOrAddUrl = '';
    switch (self.state.entityType) {
      //  公司
      case 'company':
        url = APIS.company_teams;
        entityName = 'companyId';
        pageTitle = '公司团队';
        break;
      //  项目
      case 'project':
        url = APIS.planOrAlreadyDetail;
        deleteOrAddUrl = APIS.projects_team_delete;
        entityName = 'projectId';
        pageTitle = '项目团队';
        break;
    }
    self.state.pageTitle = pageTitle;
    self.state.entityName = entityName;
    self.state.requestData.url = url;
    self.state.requestData.deleteOrAddUrl = deleteOrAddUrl;
    self.state.requestData.param[entityName] = self.state.entityId; //  团队新增会用到
    if (typeof cb === 'function') {
      cb();
    }
  },
  /* 设为经理 */
  setMain: function () {
    var self = this;
    var teamData = self.state.teamList[self.state.editJson.index];
    var url = '';
    if (self.state.entityType === 'project') {
      url = self.state.requestData.deleteOrAddUrl + teamData.id + '?' + self.state.entityName + '=' + self.state.entityId + '&staffId=' + teamData.staffId;
    } else {
      url = self.state.requestData.url + teamData.id + '?' + self.state.entityName + '=' + self.state.entityId + '&staffId=' + teamData.staffId;
    }
    nativeApi.initAjax({
      'type': 'put',
      'url': url,
      'param': '',
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.editJson.hideDe = true;
          mui.alert('经理更新成功！', '提示', function () {
            self.state.editJson.hideDe = false;
            self.team(self.state.formJson.id, self.state.formJson.flag);
          });
        } else {
          mui.alert(result.msg || '经理更新失败！', '提示', function () {
          });
        }
      }
    });
  },
  deleteTeam: function () {
    var self = this;
    var teamData = self.state.teamList[self.state.editJson.index];
    var url = '';
    if (self.state.entityType === 'project') {
      url = self.state.requestData.deleteOrAddUrl.replace('{id}', teamData.id);
    } else {
      url = self.state.requestData.url + 'del/' + teamData.id
    }
    nativeApi.initAjax({
      'type': 'delete',
      'url': url,
      'param': '',
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.editJson.hideDe = true;
          mui.alert('成员删除成功！', '提示', function () {
            self.state.editJson.hideDe = false;
            self.team(self.state.formJson.id, self.state.formJson.flag);
          });
        } else {
          mui.alert(result.msg || '成员删除失败！', '提示', function () {
          });
        }
      }
    });
  },
  teamList: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.projects_team_add,
      param: '',
      callback: function (result) {
        if (result && result.code === 200) {
          var lists = result.data;
          self.state.teamAddList = lists;
        } else {
          mui.alert('数据获取失败！', '提示', function () {
          });
        }
      }
    });
  },
  /* 团队新增 */
  teamAdd: function (cb) {
    var self = this;
    var saleStaffIdList = [];
    var lists = self.state.teamList;
    var personList = self.scroll.person.dataList;
    var arrId = [];
    var url = '';
    for (var i = 0; i < lists.length; i++) {
      arrId.push(parseInt(lists[i].staffId));
    }

    for (var m = 0; m < personList.length; m++) {
      if (personList[m].selected && arrId.indexOf(personList[m].id) === -1) {
        saleStaffIdList.push(personList[m].id);
      }
    }
    if (saleStaffIdList.length === 0) {
      return;
    }
    self.state.requestData.param.teamsList = saleStaffIdList;
    if (self.state.entityType === 'project') {
      url = self.state.requestData.deleteOrAddUrl.replace('/{id}', '');
    } else {
      url = self.state.requestData.url
    }
    nativeApi.initAjax({
      'type': 'post',
      'url': url,
      'param': self.state.requestData.param,
      callback: function (result) {
        if (result && result.code === 200) {
          if (typeof cb === 'function') {
            cb();
          }
          mui.alert('成员添加成功！', '提示', function () {
          });
        } else {
          mui.alert(result.msg || '成员添加失败！', '提示', function () {
          });
        }
      }
    });
  },
  /* 成员列表 */
  person: function (loadType) {
    var self = this;
    var obj = 'person';
    var selectList = self.state.teamList;
    var arrId = [];
    for (var i = 0; i < selectList.length; i++) {
      arrId.push(parseInt(selectList[i].staffId));
    }
    if (self.state.formJson.flag === '1') {
      self.param[obj].projectsId = self.state.formJson.id;
    } else {
      self.param[obj].projectsId = self.state.formJson.id;
    }
    nativeApi.initAjax({
      type: 'get',
      url: APIS.detpOrStaffList + 'search',
      param: self.param[obj],
      callback: function (result) {
        if (result && result.code === 200) {
          var lists = result.data;
          for (var m = 0; m < lists.length; m++) {
            if (arrId.indexOf(lists[m].id) > -1) {
              lists[m].selected = true;
            } else {
              lists[m].selected = false;
            }
          }
          self.pullList(result, loadType, obj);
          // if (loadType === 'refresh') {
          //   self.scroll[obj].hasMore = true;
          //   self.scroll[obj].dataList = lists;
          // } else if (loadType === 'loadMore') {
          //   self.scroll[obj].dataList = self.scroll[obj].dataList.concat(lists);
          // } else {
          //   if (result.data.length === 0) {
          //     self.scroll[obj].scroll.disablePullupToRefresh();
          //     self.scroll[obj].dataList = lists;
          //   } else {
          //     self.scroll[obj].scroll.enablePullupToRefresh();
          //     self.scroll[obj].scroll.refresh(true);
          //     self.scroll[obj].scroll.scrollTo(0, 0, 100);
          //     /* 列表数据 */
          //     self.scroll[obj].dataList = lists;
          //     /* 当前显示数量 */
          //     self.state.seaTotal = lists.length;
          //     self.state.seaTotalAll = result.totalSize;
          //   }
          // }
          // if (self.param[obj].pageNo === 1) {
          //   self.scroll[obj].scroll.scrollTo(0, 0, 100);
          // }
          // /* 判断是否有下一页 */
          // if (lists.length < 10 || self.scroll[obj].dataList.length === result.totalSize) {
          //   self.scroll[obj].scroll.enablePullupToRefresh();
          //   self.scroll[obj].hasMore = false;
          //   self.scroll[obj].scroll.endPullupToRefresh(!self.scroll[obj].hasMore);
          // }
          // if (loadType === 'loadMore') {
          //   self.scroll[obj].scroll.endPullupToRefresh(!self.scroll[obj].hasMore);
          // } else if (loadType === 'refresh') {
          //   if (lists.length < 10 && lists.length === result.totalSize) {
          //     self.scroll[obj].hasMore = false;
          //     self.scroll[obj].scroll.endPullupToRefresh(!self.scroll[obj].hasMore);
          //   } else {
          //     self.scroll[obj].scroll.endPulldownToRefresh();
          //     self.scroll[obj].scroll.refresh(true);
          //   }
          // }
        } else {
          mui.alert(result.msg || '数据获取失败！', '提示', function () {
          });
        }
      }
    });
  },
  pullList: function (result, loadType, obj) {
    var lists = result.data;
    var self = this;
    if (loadType === 'refresh') {
      self.scroll[obj].hasMore = true;
      self.scroll[obj].dataList = lists;
      if (obj === 'list') {
        self.state.nowTotal = lists.length;
      } else if (obj === 'search') {
        self.state.seaTotal = lists.length;
      }
    } else if (loadType === 'loadMore') {
      if (obj !== 'follow') {
        /* 跟进记录 特殊处理*/
        self.scroll[obj].dataList = self.scroll[obj].dataList.concat(lists);
      }
      if (obj === 'list') {
        self.state.nowTotal += lists.length;
      } else if (obj === 'search') {
        self.state.seaTotal += lists.length;
      }
    } else {
      if (lists.length === 0) {
        self.scroll[obj].scroll.disablePullupToRefresh();
        self.scroll[obj].dataList = lists;
        self.state.nowTotal = 0;
      } else {
        self.scroll[obj].scroll.enablePullupToRefresh();
        self.scroll[obj].scroll.refresh(true);
        self.scroll[obj].scroll.scrollTo(0, 0, 100);
        /* 列表数据*/
        self.scroll[obj].dataList = lists;
        /* 当前显示数量*/
        if (obj === 'list') {
          self.state.nowTotal = lists.length;
        } else if (obj === 'search') {
          self.state.seaTotal = lists.length;
        }
      }
    }
    if (self.param[obj].pageNo === 1) {
      self.scroll[obj].scroll.scrollTo(0, 0, 100);
    }
    /* 判断是否有下一页*/
    var arrList = self.scroll[obj].dataList;
    if (obj === 'follow') {
      /* 跟进记录 特殊处理*/
      arrList = self.scroll[obj].originalLi;
    }
    if (lists.length < 10 || arrList.length === result.totalSize) {
      self.scroll[obj].scroll.enablePullupToRefresh();
      self.scroll[obj].hasMore = false;
      self.scroll[obj].scroll.endPullupToRefresh(!self.scroll[obj].hasMore);
    }
    if (loadType === 'loadMore') {
      self.scroll[obj].scroll.endPullupToRefresh(!self.scroll[obj].hasMore);
    } else if (loadType === 'refresh') {
      if (lists.length < 10 || lists.length === result.totalSize) {
        self.scroll[obj].hasMore = false;
        self.scroll[obj].scroll.endPullupToRefresh(!self.scroll[obj].hasMore);
      } else {
        self.scroll[obj].scroll.endPulldownToRefresh();
        self.scroll[obj].scroll.refresh(true);
      }
    }
  },
};
